#include "JsonOper.h"

JsonOper::JsonOper(QObject *parent) : QObject(parent)
{

}

void JsonOper::img2Json(const std::string& fileName)
{
    cv::Mat img = imread(fileName, IMREAD_GRAYSCALE);

    vector<uchar> imgData;
    cv::imencode(".bmp", img, imgData);

    QJsonArray imgInfoArr;
    for(int i = 0; i < imgData.size(); ++i) {
        imgInfoArr.append(imgData[i]);
    }

    QJsonObject json;
    json.insert("imgInfo", imgInfoArr);
    json.insert("imgName", QString::fromStdString(fileName));

    int threshold = 50;
    json.insert("imgThreshold", threshold);

    jsonWrite_(json, "json/img.json");
}

std::string JsonOper::jsonRead(const string &jsonFileName)
{
    QFile loadFile(QString::fromStdString(jsonFileName));

    if(!loadFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "could't open projects json";
        return "";
    }

    QByteArray allData = loadFile.readAll();
    loadFile.close();
    return allData.toStdString();
}

QByteArray JsonOper::jsonRead(const string &jsonFileName, bool _Placeholder)
{
    QFile loadFile(QString::fromStdString(jsonFileName));

    if(!loadFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "could't open projects json";
        return "";
    }

    QByteArray allData = loadFile.readAll();
    loadFile.close();
    return allData;
}

void JsonOper::multiDArray2Json()
{
    vector<vector<int>> iVec{{1, 2, 3}, {4, 5, 6}};
    QJsonObject json;

    QJsonArray jsonArray;
    for(int i = 0; i < iVec.size(); ++i) {
        QJsonArray jsonCol;
        for(int j = 0; j < iVec[i].size(); ++j) {
            jsonCol.append(iVec[i][j]);
        }
        jsonArray.append(jsonCol);
    }

    QJsonDocument saveMultiArray(jsonArray);
    QFile saveFile("json/multiArray.json");
    if(!saveFile.open(QIODevice::WriteOnly)) {
        qWarning("Couldn't open save file.");
        return ;
    }

    QByteArray byteArray = saveMultiArray.toJson();
    saveFile.write(byteArray);
    saveFile.close();

    qDebug() << "save json finished";
}

void JsonOper::parseMultiArray2D(const QByteArray &byteArray)
{
    QJsonDocument loadDoc(QJsonDocument::fromJson(byteArray));

    QJsonArray jsonArray;
    if(loadDoc.isArray()) {
        jsonArray = loadDoc.array();
    }

    // get list list data
    vector<vector<int>> multiArray2D;
    multiArray2D.resize(jsonArray.size());
    for(int i = 0; i < jsonArray.size(); ++i) {
        if(jsonArray[i].isArray()) {
            QJsonValue jsonRowVal = jsonArray[i];
            QJsonArray jsonRowArray = jsonRowVal.toArray();
            multiArray2D[i].resize(jsonRowArray.size());
            for(int j = 0; j < jsonRowArray.size(); ++j) {
                multiArray2D[i][j] = jsonRowArray.at(j).toInt();
            }
        }
    }
}

void JsonOper::jsonWrite_(const QJsonObject& json,
                          const std::string& fileName)
{
    QJsonDocument document;
    document.setObject(json);

    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    QString json_str_test(byte_array);

    QFile file(QString::fromStdString(fileName));

    if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        qDebug() << "file error!";
    }

    QTextStream in(&file);
    in << json_str_test;

    file.close();
}

